package com.sun.stringescapeutils;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class OkHttpGetTestSynchronized {

    private String testGet(String url){
        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();

        //同步请求
        FutureTask task = new  FutureTask(new Callable() {
            @Override
            public Object call() throws Exception {
                OkHttpClient okHttpClient = new OkHttpClient();
                Response response = okHttpClient.newCall(request).execute();
                return response.body().string();
            }
        });
        Thread thread = new Thread(task);
        thread.start();
        Object result = null;
        try {
            result = task.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }finally {
            return (String) result;
        }

    }

    public static void main(String[] args) {
        String uri = "http://localhost:8888/tags/hot";
        OkHttpGetTestSynchronized okHttpGetTestSynchronized = new OkHttpGetTestSynchronized();
        String testGet = okHttpGetTestSynchronized.testGet(uri);
        System.out.println(testGet);
    }
}
